package com.mangohouse.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

public class LinuxCmd
{
    // 运行命令,返回值为输出内容
    public String runCmdLine(String command[])
    {
        String strRtnVal="";
        Runtime rt = Runtime.getRuntime();
        Process proc =null;
        try
        {
            proc = rt.exec(command);
            
            InputStream rstIS = proc.getInputStream();
            InputStreamReader rstISR = new InputStreamReader(rstIS);
            BufferedReader rstBR = new BufferedReader(rstISR);
            String rstLine = null;

            while ((rstLine = rstBR.readLine()) != null){
                strRtnVal +=rstLine;
            }
            if(strRtnVal == null) return null;
            return strRtnVal;
        }
        catch (Throwable t)
        {
            t.printStackTrace();
            return null;
        }
        finally{
            proc.destroy();
        }
    }
    
    public String runCmdLine(String command)
    {
        String strRtnVal="";
        Runtime rt = Runtime.getRuntime();
        Process proc =null;
        try
        {
            proc = rt.exec(command);
            
            InputStream rstIS = proc.getInputStream();
            InputStreamReader rstISR = new InputStreamReader(rstIS);
            BufferedReader rstBR = new BufferedReader(rstISR);
            String rstLine = null;

            while ((rstLine = rstBR.readLine()) != null){
                strRtnVal +=rstLine;
            }
            if(strRtnVal == null) return null;
            return strRtnVal;
        }
        catch (Throwable t)
        {
            t.printStackTrace();
            return null;
        }
        finally{
            proc.destroy();
        }
    }
    // 运行命令
    public boolean runCmd(String command)
    {
        Runtime rt = Runtime.getRuntime();
        Process proc =null;
        try
        {
            proc = rt.exec(command);
            
            InputStream rstIS = proc.getInputStream();
            InputStreamReader rstISR = new InputStreamReader(rstIS);
            BufferedReader rstBR = new BufferedReader(rstISR);
            String rstLine = null;
            System.out.println("<Info>");
            while ((rstLine = rstBR.readLine()) != null){
                System.out.println(rstLine);
            }
            System.out.println("</Info>");  
            
            InputStream errIS = proc.getErrorStream();
            InputStreamReader errISR = new InputStreamReader(errIS);
            BufferedReader br = new BufferedReader(errISR);
            String errLine = null;
            
            System.out.println("<ERROR>");
            while ((errLine = br.readLine()) != null){
                System.out.println(errLine);
            }
            System.out.println("</ERROR>");
            
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
            return true;
        }
        catch (Throwable t)
        {
            t.printStackTrace();
            return false;
        }
        finally{
            proc.destroy();
        }
    }  
    
    //删除临时文件
    public boolean delTemFile(String srcPath){
        String cmdLine = "rm "+srcPath+ "/*.tmp  -rf";
        cmdLine = cmdLine.replace("//", "/");
        String[] cmd = new String[3]; 
        cmd[0] = "/bin/sh"; 
        cmd[1] = "-c"; 
        cmd[2] = cmdLine;

        try
        {
            runCmdLine(cmd);
        }
        catch (RuntimeException e)
        {
            e.printStackTrace();
            return false;
        }

        return true;
    }
    
    //移动文件
    public  boolean moveFileInLinux(String srcPath,String descPath){

        String cmdLine = "mv "+srcPath+ " "+descPath +" -f";

        String[] cmd = new String[3]; 
        cmd[0] = "/bin/sh"; 
        cmd[1] = "-c"; 
        cmd[2] = cmdLine;

        try
        {
            runCmdLine(cmd);
            File aFile = new File(descPath);
            if(aFile.exists() && aFile.length() >0) return true;
        }
        catch (RuntimeException e)
        {
            e.printStackTrace();
            return false;
        }
        return false;
    }
   
    public static void main(String args[])
    {
        LinuxCmd test = new LinuxCmd();
//      String reqPath ="/tmp/";
//      String rspPath ="/tmp/mindy/";
//      String rscId = "1234567890";
//      test.getZipFileAndFileSizeByPath(reqPath,rscId);
//      test.getZipFileAndFileSizeByPath(reqPath, rscId, rspPath);
        //String str = "esdf/fsdkf/abc/tongqinghui.zip";
        //System.out.println("---------------------" + str.substring(0,str.lastIndexOf("/")+1));
        //test.getZipFileAndFileSizeByPath(reqPath, rspPath);
//      System.out.print(test.getZipFileAndFileSizeByPath("/home/virtual/C/C7/all/all/004285994b3d4c86a2fbc3613601e0b9/", "004285994b3d4c86a2fbc3613601e0b9"));
        //System.out.println(test.runCmdLine("ipconfig"));
        
        //test.transferByMencoder("/tmp/1.avi", "/tmp/v/1.flv","00:00:10","00:00:10");
        //test.genPic("/tmp/1.avi", "/tmp/p/1.jpg", "00:00:10", "128x96");
        
//      String filepath = "/home/g12e_rsc/ms/D/D3/C/D/164221b7cde546fcbcb942bcd8d08ee8/高三生物-王萍-试验主题复习.wmv";
//      File file = new File(filepath);
//      if(file.isFile())
//      {
//          System.out.println("-------------filesize:"+file.length());
//      }
    }
}
